import { debugLogger } from '../utils/index.js'

export function jsError(_this, isJsErrorFlag) {
  if (!isJsErrorFlag) {
    debugLogger.call(_this, 'config.dataInfo值为false, 跳过数据埋点')
    return;
  }
  // 资源加载错误捕获
  window.addEventListener('error', e => {
    const target = e.target
    if (!target) return
    if (target.src || target.href) {
      const url = target.src || target.href
      _this.sendInfo.JS_error.static_err.push({
        srcPath: url,
        resourceType: target.tagName
      })
    }
  }, true)
  window.onerror = function (msg, url, line, col, error) {
    debugLogger.call(_this, "Error: ", msg + "\nURL: " + url + "\nLine: " + line + "\nColumn: " + col + "\nStackTrace: " + error)
    _this.sendInfo.JS_error.onerror.push({
      msg,
      url,
      line,
      col,
      error
    })
    return true;
  };
  // 可拓展监听框架自身异常报错
  if (_this._CONFIG.Plugin) {
    let vue = _this._CONFIG.Plugin
    if (!vue.version) return
    vue.config.errorHandler = (err, vm, info) => {
      _this.sendInfo.JS_error.frame_err.push({
        msg: err.message,
        info //Vue特定的错误信息，例如错误发生的生命周期、错误发生的事件
      })
      throw err.message
    }  
  }

}








